package com.im.service.purchasePlan.smo;

import com.im.MDA;
import com.im.service.common.smo.ICommonSMO;
import com.im.service.dto.BaseException;
import com.im.service.dto.FileUpload;
import com.im.service.dto.ImplementProject;
import com.im.service.dto.PurchasePlan;
import com.im.service.projectInfo.smo.IProjectInfoQuerySMO;
import com.im.service.purchasePlan.dao.IPurchasePlanOperDAO;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class PurchasePlanOperSMOImpl implements IPurchasePlanOperSMO{

    private static final Logger logger = LogManager.getLogger(PurchasePlanOperSMOImpl.class);

    @Autowired
    private IPurchasePlanOperDAO purchasePlanOperDAO;
    @Autowired
    private ICommonSMO commonSMO;
    @Autowired
    private IProjectInfoQuerySMO projectInfoQuerySMO;

    @Override
    public void addPurchasePlan(PurchasePlan purchasePlan) throws Exception{
        // 1.新增规划工程
        String purchPlanId =UUID.randomUUID().toString();
        purchasePlan.setPurchPlanId(purchPlanId);
        ImplementProject req = new ImplementProject();
        req.setImpleProjId(purchasePlan.getImpleProjId());
        ImplementProject implementProject = projectInfoQuerySMO.queryImpleProjDetail(req);
        String purchPlanCode = commonSMO.querySeqForRule("2",implementProject.getImpleProjId());
        purchasePlan.setPurchPlanCode(purchPlanCode);
        purchasePlanOperDAO.addPurchasePlan(purchasePlan);

    }

    @Override
    public void updatePurchasePlan(PurchasePlan purchasePlan) throws Exception{
        purchasePlanOperDAO.updatePurchasePlan(purchasePlan);
    }

    @Override
    public void updatePurchasePlanAll(PurchasePlan purchasePlan) throws Exception {
        purchasePlanOperDAO.updatePurchasePlanAll(purchasePlan);
    }

    @Override
    public void deletePurchasePlan(PurchasePlan purchasePlan) throws Exception{
        purchasePlanOperDAO.deletePurchasePlan(purchasePlan);
    }

    @Override
    public void deletePurchasePlanByIds(Map<String,Object> reqMap) throws Exception{
        purchasePlanOperDAO.deletePurchasePlanByIds(reqMap);
    }

    @Override
    public String uploadFileToPurchasePlan(String inJson) throws Exception {
       try{
            String purchPlanId = JsonUtil.obtainValueFromJsonObject(inJson, "purchPlanId");
            String fileAttr = JsonUtil.obtainValueFromJsonObject(inJson, "fileAttr");
            String fileType = JsonUtil.obtainValueFromJsonObject(inJson, "fileType");
            //保存附件信息
            JSONObject param = JSONObject.fromObject(inJson);
            if (param.containsKey("fileList")) {
                JSONArray fileListArray = param.getJSONArray("fileList");
                if (fileListArray != null) {
                    List<FileUpload> fileUploads = new ArrayList<FileUpload>();
                    for (Object fileUpload : fileListArray) {
                        JSONObject fileUploadJson = JSONObject.fromObject(fileUpload);
                        FileUpload fileUploadInfo = new FileUpload();
                        String uploadId = UUID.randomUUID().toString();
                        String uploadType = (String) MDA.FILE_UPLOAD_TYPE.get(fileUploadJson.getString("attachSuffix").toLowerCase());
                        fileUploadInfo.setUploadId(uploadId);
                        fileUploadInfo.setUploadName(fileUploadJson.getString("attachFullName"));
                        fileUploadInfo.setUploadObjType("220");
                        fileUploadInfo.setUploadObjId(purchPlanId);
                        fileUploadInfo.setUploadType(uploadType);
                        fileUploadInfo.setFileSuffix(fileUploadJson.getString("attachSuffix"));
                        fileUploadInfo.setFileAttr(fileAttr);
                        fileUploadInfo.setFileType(fileType);
                        fileUploadInfo.setUploadSize(fileUploadJson.getLong("attachSize"));
                        fileUploadInfo.setFilePath(fileUploadJson.getString("attachPath"));
                        fileUploadInfo.setUploadDate(fileUploadJson.getString("uploadDt"));
                        fileUploads.add(fileUploadInfo);
                    }
                    purchasePlanOperDAO.insertFileUploadInfos(fileUploads);
                }
            }
            return ReturnsUtil.returnSuccess(new JSONObject()).toString();
        } catch (BaseException e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.toString());
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(ReturnsUtil.getStackTrackMessage(e));
            }
            return ReturnsUtil.returnFailure(e.toString());
        }
    }
}
